package cn.stylefeng.guns.modular.robot.service.impl;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.robot.entity.Group;
import cn.stylefeng.guns.modular.robot.entity.GroupRelationship;
import cn.stylefeng.guns.modular.robot.entity.enums.GroupType;
import cn.stylefeng.guns.modular.robot.mapper.GroupRelationshipMapper;
import cn.stylefeng.guns.modular.robot.service.GroupRelationshipService;
import cn.stylefeng.guns.modular.robot.service.GroupService;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class GroupRelationshipServiceImpl extends ServiceImpl<GroupRelationshipMapper, GroupRelationship> implements GroupRelationshipService {

    @Resource
    private GroupService groupService;

    @Override
    public void bindRelationship(GroupRelationship groupRelationship) {
        if (groupRelationship.getSendGroupId() == null || groupRelationship.getReceiveGroupId() == null) {
            throw new ServiceException(BizExceptionEnum.GROUP_RELATIONSHIP_ID_EMPTY);
        }
        Group upGroup = groupService.getGroupByTgGroupId(groupRelationship.getReceiveGroupId());
        if (Objects.isNull(upGroup)) {
            throw new ServiceException(BizExceptionEnum.GROUP_NO_EXISTS);
        }
        if(GroupType.DF.equals(upGroup.getType())){
            throw new ServiceException(BizExceptionEnum.DF_GROUP_NO);
        }
        Group lowerGroup = groupService.getGroupByTgGroupId(groupRelationship.getSendGroupId());
        if (Objects.isNull(lowerGroup)) {
            throw new ServiceException(BizExceptionEnum.GROUP_NO_EXISTS);
        }
        if(GroupType.DF.equals(lowerGroup.getType())){
            throw new ServiceException(BizExceptionEnum.DF_GROUP_NO);
        }
        String account = ShiroKit.getUserNotNull().getAccount();
        if (!account.equals(upGroup.getCreateBy()) || !account.equals(lowerGroup.getCreateBy())) {
            throw new ServiceException(BizExceptionEnum.NO_GROUP_ERROR);
        }
        if (Boolean.FALSE.equals(upGroup.getUpGroup())) {
            throw new ServiceException(BizExceptionEnum.RE_BIND_ERROR);
        }
        if (!Objects.isNull(lowerGroup.getUpGroup())) {
            throw new ServiceException(BizExceptionEnum.RE_BIND_ERROR);
        }
        upGroup.setUpGroup(true);
        groupService.updateById(upGroup);
        lowerGroup.setUpGroup(false);
        groupService.updateById(lowerGroup);

        groupRelationship.setCreateTime(new Date());
        groupRelationship.setCreateBy(account);
        save(groupRelationship);
    }

    @Override
    public void unbindRelationship(Long sendGroupId) {
        List<GroupRelationship> groupRelationships = lambdaQuery()
                .eq(GroupRelationship::getSendGroupId, sendGroupId)
                .list();
        if (CollectionUtils.isEmpty(groupRelationships)) {
            return;
        }
        GroupRelationship groupRelationship = groupRelationships.get(0);
        removeByIds(groupRelationships.stream().map(GroupRelationship::getId).collect(Collectors.toSet()));

        UpdateWrapper<Group> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("tg_group_id", groupRelationship.getSendGroupId());
        updateWrapper.set("up_group", null);
        groupService.update(updateWrapper);

        groupRelationships = lambdaQuery()
                .eq(GroupRelationship::getReceiveGroupId, groupRelationship.getReceiveGroupId())
                .list();
        if (!CollectionUtils.isEmpty(groupRelationships)) {
            return;
        }
        updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("tg_group_id", groupRelationship.getReceiveGroupId());
        updateWrapper.set("up_group", null);
        groupService.update(updateWrapper);
    }
}
